package com.pearl.jacksoncore.demo.databind.anno;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.annotation.JsonTypeResolver;
import com.fasterxml.jackson.databind.cfg.MapperConfig;
import com.fasterxml.jackson.databind.jsontype.impl.StdTypeResolverBuilder;

/**
 * @author TD
 * @version 1.0
 * @date 2024/3/28
 */
public class Data {
    private Long key;

    @JsonCreator
    Data(@JsonProperty("key") Long key) {
        this.key = key;
    }

    @JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, property = "@class")
    @JsonTypeResolver(MyTypeResolverBuilder.class)
    public long key() {
        return key;
    }

    static class MyTypeResolverBuilder extends StdTypeResolverBuilder {
        // 允许显示类型
        @Override
        protected boolean allowPrimitiveTypes(MapperConfig<?> config,
                                              JavaType baseType) {
            return true;
        }
    }
}
